'######################################################################################
'Author  :         Sumit Gupta 
'Company :				 Vikasumit
'Date    :         7th July 2005
'--------------------------------------------------------------------------------------
'Working / Note on Class : 
'clsLog Class is for Logging the Debug Comments in the File
'All caller will create the Instance of the clsLog with there class Name
'Version of Class etc and it will log the data in the Debuglog Folder of 
'Application path
'######################################################################################

Imports System.IO

Public Class clsLog

	'Member Data Section

#Region "Private Temparory Varibles for Properties"
	Private tmp_CallerClass As String
	Private tmp_CallerClassVersion As String
	Private tmp_LogFilePath As String
	Private tmp_DoLogging As Boolean
	Private tmp_AddDateTimeStamp As Boolean
  Private Const CON_CLASSVERSION As String = "1.0.20050707"  'Define the Class Version
#End Region

#Region "Public Event Declaration"
	'WrongPropertyValue is call from a Set Sub of a Property Whenever it encounter a Error in the Property Value
	Public Event WrongPropertyValue(ByVal PropertyName As String, ByVal Message As String)
	Public Event UnknownException(ByVal Caller As String, ByVal Message As String)
#End Region

#Region "Public Property"

	'Get the Class Version
	Public ReadOnly Property ClassVersion() As String
		Get
			Return CON_CLASSVERSION
		End Get
	End Property

	'Get the CallerClass
	Public ReadOnly Property CallerClass() As String
		Get
			Return tmp_CallerClass
		End Get
	End Property

	'Get the CallerClass Version
	Public ReadOnly Property CallerClassVersion() As String
		Get
			Return tmp_CallerClassVersion
		End Get
	End Property

	'Get/Set the LogFile if not used the Default "Debuglog/<callerclass>.log" File
	Public Property LogFilePath() As String
		Get
			Return tmp_LogFilePath
		End Get
		Set(ByVal Value As String)
			Dim temp() As String
      temp = Value.Split(CChar("."))
			If temp.Length < 2 Then
				RaiseEvent WrongPropertyValue("LogFilePath", "Invalid File Name")
			Else
				tmp_LogFilePath = Value
			End If
		End Set
	End Property

	'Allow Logging or not
	Public Property DoLogging() As Boolean
		Get
			Return tmp_DoLogging
		End Get
		Set(ByVal Value As Boolean)
			tmp_DoLogging = Value
		End Set
	End Property

	'If Time Stamp is added in log or not
	Public Property AddDateTimeStamp() As Boolean
		Get
			Return tmp_AddDateTimeStamp
		End Get
		Set(ByVal Value As Boolean)
			tmp_AddDateTimeStamp = Value
		End Set
	End Property
#End Region

	'Behaviour /Member Function Section

#Region "Class Constructors"
	'Default Constructor with All Default Settings
	Public Sub New()
		tmp_LogFilePath = System.AppDomain.CurrentDomain.BaseDirectory & "DebugLog\DebugComments.Log"
		tmp_DoLogging = True
		tmp_CallerClass = "Unknown"
		tmp_CallerClassVersion = "Unknown"
	End Sub

	'Constructor with Class Information
	Public Sub New(ByVal sCallerClass As String, ByVal sCallerClassVersion As String)
		tmp_CallerClass = sCallerClass
		tmp_CallerClassVersion = sCallerClassVersion
		tmp_DoLogging = True
		tmp_LogFilePath = System.AppDomain.CurrentDomain.BaseDirectory & "DebugLog\" & tmp_CallerClass & ".Log"
	End Sub

#End Region

#Region "Public Functions"

	'Write the Comment to the Log
  Public Sub Write(ByVal sComment As String)
    If Me.DoLogging Then
      Dim File As FileStream
      Dim fw As StreamWriter
      Dim ClassInfo As String
      ClassInfo = " : " & tmp_CallerClass & " ( " & tmp_CallerClassVersion & " ) "
      Try
        File = New FileStream(tmp_LogFilePath, FileMode.Append, FileAccess.Write)
        fw = New StreamWriter(File)
      Catch ex As Exception
        MsgBox("Unable to open the Log File")
        Exit Sub
      End Try


      Try
        If tmp_AddDateTimeStamp Then
          fw.WriteLine(Now & ClassInfo & " : " & sComment)
        Else
          fw.WriteLine(ClassInfo & sComment)
        End If
      Catch ex As Exception
        RaiseEvent UnknownException("Write", "An Unknwon Error has occur during Write Operation")
      End Try

      fw.Close()
      File.Close()
    End If
  End Sub

#End Region

End Class